/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import com.jgoodies.forms.layout.CellConstraints;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import main.WinkelApplication;
import model.Klant;

import com.jgoodies.forms.layout.FormLayout;


/**
 *
 * @author Jack Sierkstra
 */
public class CustomerAdd extends JPanel{
    
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    
    TextField naamtext;
    TextField adrestext;
    TextField postcodetext;
    TextField woonplaatstext;
    
    public CustomerAdd() {
        
      super();
      setLayout(new BorderLayout());
      addTitle();
      addpanel();
      addButtonPane();
     
    }
    
    
    public void addTitle() {
        JLabel titel = new JLabel("Klant toevoegen", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.getInstance().computeFontSize(2));
        
        JPanel frame = new JPanel();
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));

        frame.add(titel);
        
        titel.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        add(frame, BorderLayout.PAGE_START);
    }
    
    public void addButtonPane() {
           
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(dim.width, 40));
        
        JButton opslaan = new JButton("Klant toevoegen");
        
        opslaan.setPreferredSize(new Dimension(150,25));
        opslaan.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             
              saveCustomer(naamtext.getText(), adrestext.getText(), postcodetext.getText(), woonplaatstext.getText());
              
              
          }
        });
        
        JButton terug = new JButton("Annuleren");
        
        terug.setPreferredSize(new Dimension(150,25));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {
            javax.swing.SwingUtilities.getWindowAncestor(CustomerAdd.this).dispose();
          }
        });

        
        frame.add(opslaan);
        frame.add(terug);
        
        add(frame, BorderLayout.PAGE_END);
         
    }

    public void addpanel() {
        
        
          FormLayout layout = new FormLayout(
                "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
              + "right:max(40dlu;pref), 3dlu, 70dlu",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p");
                
        JPanel panel = new JPanel(layout);
        panel.setBorder(Borders.DIALOG_BORDER);
       

        naamtext = new TextField();
        adrestext = new TextField();
        postcodetext = new TextField();
        woonplaatstext = new TextField();
        
        JLabel naam = new JLabel("Naam:");
        JLabel adres = new JLabel("Adres:");
        JLabel postcode = new JLabel("Postcode:");
        JLabel woonplaats = new JLabel("Woonplaats:");
        
        naam.setFont(WinkelApplication.computeFontSize(3));
        adres.setFont(WinkelApplication.computeFontSize(3));
        postcode.setFont(WinkelApplication.computeFontSize(3));
        woonplaats.setFont(WinkelApplication.computeFontSize(3));
        
        
        // Fill the table with labels and components.
        CellConstraints cc = new CellConstraints();
        
        panel.add(naam,             cc.xy   (1,  3));
        panel.add(naamtext,         cc.xyw  (3,  3, 5));
        panel.add(adres,            cc.xy   (1,  5));
        panel.add(adrestext,        cc.xyw  (3,  5, 5));
        panel.add(postcode,         cc.xy   (1, 7));
        panel.add(postcodetext,     cc.xyw  (3,  7, 5));
        panel.add(woonplaats,       cc.xy   (1, 9));
        panel.add(woonplaatstext,   cc.xyw  (3,  9, 5));
  
         
          
        add(panel, BorderLayout.CENTER);
    }
    
    private Component createSeparator(String text) {
        return DefaultComponentFactory.getInstance().createSeparator(text);
    }

    public void saveCustomer(String naam, String adres, String postcode, String woonplaats) {
       
        
        Klant klant = new Klant(naam, adres, postcode, woonplaats);
        
        WinkelApplication.getQueryManager().addCostumer(klant);
        
        
        javax.swing.SwingUtilities.getWindowAncestor(CustomerAdd.this).dispose();
        WinkelApplication.getInstance().showPanel(new view.CustomerPanel());
        
    }
    
    
 
}
